local CopyL = require("copy.CopyL")
local CopyDefine = require("copy.Define")
local ObjHuman = require("core.ObjHuman")
local Timer = require("core.Timer")
local Lang = require("common.Lang")
--local Util = require("common.Util")
local Broadcast = require("broadcast.Broadcast")
local SceneEvent = require("core.SceneEvent")
local DailyActFL = require("activity.DailyActFL")
local ActDefine = require("activity.Define")
local Copy = require("copy.CopyL")
local RoleFFI = require("role.RoleFFI")
local HtmlUtil = require("common.HtmlUtil")

RoomStatus = RoomStatus or {}

function checkEnter(human)
    local nStartTime, nEndTime = getActTime()
    if nStartTime <= 0 then
        Broadcast.sendBroadcast(human, Lang.DOWN, Lang.GMTX_NOT_START)  --活动暂未开启
        return false
    end

    if nEndTime <= 0 then
        Broadcast.sendBroadcast(human, Lang.DOWN, Lang.GMTX_NOT_START)  --活动暂未开启
        return false
    end

    return true
end

--得到开始时间
function getActTime()
    local nActData = DailyActFL.onGetActData(ActDefine.ACT_TYPE_GMTX_ACT)
    if not nActData or nActData.status ~= ActDefine.ACT_STATE_RUNNING then
        return 0, 0
    end
    return nActData.startTime, nActData.endTime
end

function onActBegin()
    if actState == ActDefine.ACT_STATE_RUNNING then
        Broadcast.sendBroadcast2World(Lang.GROSSIP + Lang.UP, string.format(Lang.DAILY_ACTIVITY_GMTX_OPEN, "董卓", copyID))
    end
end

function onRoomBegin(copyID, roomID)
    RoomStatus[roomID] = nil 
    RoomStatus[roomID] = {status = 0}
end

function onRoomFinish(copyID, roomID)
    if RoomStatus[roomID].status ~= 1 then
        RoomStatus[roomID].status = 1
        local nHumanList = CopyL.getCopyHumanList(copyID, roomID)
        for k, v in ipairs(nHumanList) do
            local nHuman = ObjHuman.onlineUuid[v._id]
            if nHuman then
                local team = nHuman.db.teamUuid
                local teamLen = nHuman.db.teamUuidLen
                if teamLen ~= 0 then
                    -- 组队
                    local humanInCopy = Copy.isCopyBySceneID(nHuman.scene_id)
                    if team[0] then
                        local member = ObjHuman.onlineUuid[RoleFFI.ffi_string(team[0].uuid)]
                        if member then
                            Broadcast.sendBroadcast2World(Lang.GROSSIP + Lang.UP, string.format(Lang.GMTX_FINISH, HtmlUtil.fontName(member.name, member._id)))
                            break
                        end
                    end
                end

            end
        end
    end
end

function onRoomClear(copyID, roomID)
    RoomStatus[roomID] = nil 
end

DailyActFL.onRegisterCallBack(ActDefine.ACT_TYPE_GMTX_ACT, nil, onActBegin, nil)
